package com.qc.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qc.servicebase.config.GuliException;
import com.qc.serviceedu.entity.Excel.Listener.SubjectExcelListener;
import com.qc.serviceedu.entity.Excel.SubjectData;
import com.qc.serviceedu.entity.EduSubject;
import com.qc.serviceedu.mapper.EduSubjectMapper;
import com.qc.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.serviceedu.entity.vo.OneSubjectVo;
import com.qc.serviceedu.entity.vo.TwoSubjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author AltmanQc
 * @since 2020-12-23
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"课堂添加分类失败");
        }

    }

    //课程分类列表(树型)
    @Override
    public List<OneSubjectVo> getTreeSubject() {
        //1.查出所有一级id
        QueryWrapper<EduSubject> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("parent_id",0);
        List<EduSubject> oneSubject = this.baseMapper.selectList(oneWrapper);
        //2.查出所有二级分类
        QueryWrapper<EduSubject> twoWrapper = new QueryWrapper<>();
        oneWrapper.ne("parent_id",0);
        List<EduSubject> twoSubject = this.baseMapper.selectList(twoWrapper);
        //封装一级分类
        //创建List集合，用于存储最终的封装
        List<OneSubjectVo> finalSubjectList = new ArrayList<>();
        for (int i = 0; i < oneSubject.size(); i++) {
            EduSubject eduSubject = oneSubject.get(i);
            OneSubjectVo oneSubjectVo = new OneSubjectVo();
            BeanUtils.copyProperties(eduSubject,oneSubjectVo);
            finalSubjectList.add(oneSubjectVo);
            //封装二级分类
            List<TwoSubjectVo> finalTwoSubjectList = new ArrayList<>();
            for (int j = 0; j < twoSubject.size(); j++) {
                //获取每个二级分类
                EduSubject subjectTwo = twoSubject.get(j);
                //判断二级分类parent_id和一级分类是否一样
                if (subjectTwo.getParentId().equals(eduSubject.getId())){
                    TwoSubjectVo twoSubjectVo = new TwoSubjectVo();
                    BeanUtils.copyProperties(subjectTwo,twoSubjectVo);
                    finalTwoSubjectList.add(twoSubjectVo);
                }
            }
            //把下面的二级分类放到二级分类
            oneSubjectVo.setChildren(finalTwoSubjectList);
        }
        return finalSubjectList;
    }
}
